package com.workplan.client.widgets.workdays;

import java.util.HashSet;
import java.util.Set;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import com.general.client.callback.Callback;
import com.google.gwt.user.client.Window;
import com.workplan.shared.datamodel.workweek.Workweek.WorkDays;

public class WorkdaysPresenterImpl extends
		WidgetPresenter<WorkdaysPresenter.Display> implements WorkdaysPresenter {

	public WorkdaysPresenterImpl(Display display, EventBus eventBus) {
		super(display, eventBus);
	}

	@Override
	protected void onBind() {
		// TODO bring data from server
		// add handlers to persist data on change
		HashSet<WorkDays> initSelectedDays = new HashSet<WorkDays>();
		initSelectedDays.add(WorkDays.MONDAY);
		initSelectedDays.add(WorkDays.TUESDAY);
		initSelectedDays.add(WorkDays.WEDNESDAY);
		initSelectedDays.add(WorkDays.THURSDAY);
		initSelectedDays.add(WorkDays.FRIDAY);
		display.setSelectedWorkdays(initSelectedDays);
		display.onWeekStartDayChanged(new Callback<WorkDays>() {

			@Override
			public void onItemChanged(WorkDays item) {
				Window.alert("week starts on:" + item.name());
			}
		});

		display.onSelectedDaysChanged(new Callback<Set<WorkDays>>() {

			@Override
			public void onItemChanged(Set<WorkDays> item) {
				StringBuilder s = new StringBuilder();
				for (WorkDays wd : item) {
					s.append(wd.name() + ",");
				}
				Window.alert(s.toString());
			}
		});
	}

	@Override
	protected void onUnbind() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onRevealDisplay() {
	}

	@Override
	public void onSelectedDaysChanged(Callback<Set<WorkDays>> callback) {
		display.onSelectedDaysChanged(callback);
	}

	@Override
	public void onWeekStartDayChanged(Callback<WorkDays> callback) {
		display.onWeekStartDayChanged(callback);
	}

}
